package com.newviews.tcmip.web.controller;

import com.newviews.tcmip.common.config.ConfigProperties;
import com.newviews.tcmip.common.constant.CommonConsts;
import com.newviews.tcmip.common.model.PageModel;
import com.newviews.tcmip.common.model.ResultModel;
import com.newviews.tcmip.common.utils.ExportExcelUtils;
import com.newviews.tcmip.common.utils.UUIDGenerator;
import com.newviews.tcmip.pojo.vo.export.EtcmCommonExportReqVO;
import com.newviews.tcmip.pojo.vo.export.EtcmPxHerbExportReqVO;
import com.newviews.tcmip.pojo.vo.export.EtcmPxHerbIngrExportReqVO;
import com.newviews.tcmip.pojo.vo.export.EtcmPxTargetExportReqVO;
import com.newviews.tcmip.service.inf.EtcmDiseaseExServiceInf;
import com.newviews.tcmip.service.inf.EtcmPxHerbServiceInf;
import com.newviews.tcmip.web.vo.etcmpx.PxHerbStatisticsRespVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 处方-中草药关系表 前端控制器
 * </p>
 *
 * @author lxc
 * @since 2024-03-01
 */
@RestController
@RequestMapping("/etcm/prescription/herb")
@Api(tags = "处方草药关联信息")
public class EtcmPxHerbController {

    @Autowired
    private EtcmPxHerbServiceInf etcmPxHerbService;

    @Autowired
    private ConfigProperties configProperties;

    @Resource
    private EtcmDiseaseExServiceInf etcmDiseaseExServiceInf;

    @GetMapping("/info")
    @ApiOperation("获取处方草药信息")
    public ResultModel<PageModel<Map<String,String>>> getInfo(@RequestParam String prescriptionId,
                                                              @RequestParam String uiTypeCode,
                                                              @RequestParam String langCode,
                                                              @RequestParam(required = false) String content,
                                                              @RequestParam(required = false) String herbMeridians,
                                                              @RequestParam(required = false) String herbProperty,
                                                              @RequestParam(required = false) String herbFlavor,
                                                              @RequestParam(required = false, defaultValue = "1") Integer pageNo,
                                                              @RequestParam(required = false, defaultValue = "10") Integer pageSize,
                                                              @RequestParam(required = false, defaultValue = "desc") String sortType){
        return etcmPxHerbService.getInfo(prescriptionId,content,herbMeridians,herbProperty,herbFlavor,sortType, uiTypeCode, langCode, pageNo, pageSize);
    }

    @GetMapping("/herb/statistics")
    @ApiOperation("获取处方草药统计")
    public ResultModel<PxHerbStatisticsRespVo> getHerbStatistics(@RequestParam String prescriptionId,
                                                                 @RequestParam String uiTypeCode,
                                                                 @RequestParam String langCode,
                                                                 @RequestParam String herbFlavor,
                                                                 @RequestParam String herbProperty,
                                                                 @RequestParam String herbMeridians,
                                                                 @RequestParam(required = false) String content){
        return etcmPxHerbService.herbStatistics(prescriptionId,content,uiTypeCode,langCode,herbMeridians,herbProperty,herbFlavor);
    }

    @PostMapping("/download")
    @ApiOperation(value = "处方草药关联信息下载")
    public void downloadHerb(
            @ApiIgnore HttpServletResponse response,
            @RequestBody @Valid EtcmPxHerbExportReqVO reqVO) {
        try {

            List<Map<String, String>> pxList = etcmPxHerbService.getPxHerbDownload(reqVO.getPxId(), reqVO.getHerbFlavor(),reqVO.getHerbProperty(),reqVO.getHerbMeridians(), reqVO.getContent(), reqVO.getLangCode(), reqVO.getUiTypeCode(),reqVO.getSortType());
            etcmDiseaseExServiceInf.getDownload(response,CommonConsts.UI_HERB,reqVO.getLangCode(),reqVO.getExportType(),pxList);

        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    @PostMapping("/ingr/download")
    @ApiOperation(value = "处方草药成分信息下载")
    public void downloadIngr(
            @ApiIgnore HttpServletResponse response,
            @RequestBody @Valid EtcmPxHerbIngrExportReqVO reqVO) {
        String path="";
        try {
            List<Map<String, String>> pxDetails = etcmPxHerbService.getIngredientList(reqVO.getMetabolicStability(), reqVO.getTransportMode(), reqVO.getHalfLifePeriod(), reqVO.getContent(), reqVO.getLangCode(), reqVO.getUiTypeCode(), reqVO.getPxId());
            File file=new File(configProperties.getFileExportPath());
            if(!file.exists()){
                file.mkdirs();
            }
            if(reqVO.getExportType().equals(CommonConsts.STR_COMMON_YES)){
                path= configProperties.getFileExportPath()+ UUIDGenerator.getDateNumber()+CommonConsts.EXPORT_FILE_FORMAT_1;
                ExportExcelUtils.writeListOfMapsToCSV(pxDetails, path);
            }else {
                path= configProperties.getFileExportPath()+ UUIDGenerator.getDateNumber()+CommonConsts.EXPORT_FILE_FORMAT_2;
                ExportExcelUtils.writeListOfMapsToTxt(pxDetails, path);
            }
            InputStream stream = new FileInputStream(path);
            ServletOutputStream out = response.getOutputStream();
            byte buff[] = new byte[1024];
            int length = 0;
            while ((length = stream.read(buff)) > 0) {
                out.write(buff, 0, length);
            }
            stream.close();
            out.close();
            out.flush();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            new File(path).delete();
        }
    }

    @PostMapping("/target/download")
    @ApiOperation(value = "处方成分靶向信息下载")
    public void downloadTarget(
            @ApiIgnore HttpServletResponse response,
            @RequestBody @Valid EtcmPxTargetExportReqVO reqVO) {
        String path="";
        try {
            List<Map<String, String>> pxDetails = etcmPxHerbService.getTarget(reqVO.getTargetType(), reqVO.getContent(),reqVO.getLangCode(),reqVO.getUiTypeCode(),reqVO.getPxId());
            File file=new File(configProperties.getFileExportPath());
            if(!file.exists()){
                file.mkdirs();
            }
            if(reqVO.getExportType().equals(CommonConsts.STR_COMMON_YES)){
                path= configProperties.getFileExportPath()+ UUIDGenerator.getDateNumber()+CommonConsts.EXPORT_FILE_FORMAT_1;
                ExportExcelUtils.writeListOfMapsToCSV(pxDetails, path);
            }else {
                path= configProperties.getFileExportPath()+ UUIDGenerator.getDateNumber()+CommonConsts.EXPORT_FILE_FORMAT_2;
                ExportExcelUtils.writeListOfMapsToTxt(pxDetails, path);
            }
            InputStream stream = new FileInputStream(path);
            ServletOutputStream out = response.getOutputStream();
            byte buff[] = new byte[1024];
            int length = 0;
            while ((length = stream.read(buff)) > 0) {
                out.write(buff, 0, length);
            }
            stream.close();
            out.close();
            out.flush();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            new File(path).delete();
        }
    }
}
